package controller;

import net.sf.json.JSONObject;
import pojo.Cate;
import pojo.User;
import service.CateManagement;
import service.LoginManagement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: zen
 * @date: 2020/9/8
 * @Title: EditCate
 * @Description: 编辑美食
 */
@WebServlet("/editCate")
public class EditCate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text=html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        // 获取前端发送过来的JSON格式字符串
        String cateInfo = req.getParameter("cate");
        // 将JSON格式字符串转换为JSON对象
        JSONObject jsonObject = JSONObject.fromObject(cateInfo);
        // 利用API将JSON对象转为JAVA对象
        Cate cate = (Cate) JSONObject.toBean(jsonObject, Cate.class);
        // 执行更新操作之后给出提出，不论是否成功更新
        if (CateManagement.editCate(cate)) {
            out.write("ok");
        } else {
            out.write("error");
        }
        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
